﻿using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Zhongy
{
    /// <summary>
    /// 用户类
    /// </summary>
    public class UserModel
    {
        public string ID { private set; get; }
        public string Name { private set; get; }
        public float Battery { private set; get; }//剩余电量
        public int Occ { private set; get; }// 0:not use; 1:self use; 2:other use;
        private int ready;//为1时表示已准备好可以传输数据
        public bool Ready
        {
            get { return ready == 1; }
        }

        public UserModel(string ID)
        {
            if (!string.IsNullOrEmpty(this.ID) && !this.ID.Equals(ID))
            {
                this.Name = string.Empty;
                this.Battery = 0;
                this.Occ = 0;
                this.ready = 0;
            }

            this.ID = ID;
        }

        public void SyncOnLineState(RevOnLineState msg)
        {
            this.ID = msg.ID;
            this.Name = msg.Name;
            this.Battery = msg.BATTERY;
            this.Occ = msg.Occ;
            this.ready = msg.Ready;

            //Debug.Log(msg.Name);
        }
    }

    /// <summary>
    /// 用户管理类
    /// </summary>
    public class UserCache
    {
        private Dictionary<string, UserModel> dicID2User = new Dictionary<string, UserModel>();
        private Dictionary<string, ClientPer> dicID2Client = new Dictionary<string, ClientPer>();

        public UserModel GetUser(string id)
        {
            return IsExsitUser(id) ? dicID2User[id] : null;
        }

        public bool IsExsitUser(UserModel user)
        {
            if (user == null || string.IsNullOrEmpty(user.ID))
                Debug.LogError("数据不正常");

            return IsExsitUser(user.ID);
        }

        public bool IsExsitUser(string id)
        {
            return dicID2User.ContainsKey(id);
        }

        public void SysncOnlineUsersState(ClientPer client, List<RevOnLineState> msgLst)
        {
            if(msgLst==null || msgLst.Count<=0)
                return;

            IDictionary<string,bool> record = new Dictionary<string, bool>();
            if (dicID2User != null && dicID2User.Count > 0)
                record = dicID2User.Keys.ToDictionary(item => item, item => false);

            foreach (var msg in msgLst)
            {
                if (IsExsitUser(msg.ID))
                    record[msg.ID] = true;

                SyncOnlineUserState(client, msg);
            }

            foreach (var item in record)
            {
                if (item.Value == false)
                {
                    OfflineUser(item.Key);
                }
            }
        }

        public void SyncOnlineUserState(ClientPer client, RevOnLineState msg)
        {
            if (client == null || string.IsNullOrEmpty(msg.ID))
            {
                Debug.LogError("数据无效");
                return;
            }

            if (!dicID2User.ContainsKey(msg.ID))
            {
                Debug.LogWarning(string.Format("同步没加入的用户数据{0}", msg.ID));
                OnlineUser(client, new UserModel(msg.ID));
            }

            dicID2User[msg.ID].SyncOnLineState(msg);
        }

        private void OnlineUser(ClientPer client, UserModel user)
        {
            if (user == null || string.IsNullOrEmpty(user.ID))
                Debug.LogError("数据不正常");

            if (IsExsitUser(user.ID))
                Debug.LogWarning(string.Format("已存在该用户（ID:{0}）", user.ID));

            dicID2User[user.ID] = user;

            dicID2Client[user.ID] = client;
        }


        private void OfflineUser(UserModel user)
        {
            if (user == null || string.IsNullOrEmpty(user.ID))
            {
                Debug.LogError("数据不正常");
                return;
            }

            OfflineUser(user.ID);
        }

        private void OfflineUser(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                Debug.LogError("id 为空值");
            }

            if (IsExsitUser(id))
            {
                dicID2User.Remove(id);

                dicID2Client.Remove(id);
                Debug.Log(string.Format("id为{0}的用户已从缓存删除", id));
            }
            else
            {
                Debug.Log(string.Format("id为{0}的用户不存在缓存中", id));
            }
        }

        public bool IsExsitClient(ClientPer client)
        {
            if (client == null)
            {
                Debug.LogError("输入的 client 数据不正常");
                return false;
            }

            return dicID2Client.ContainsValue(client);
        }

        public void OfflineUser(ClientPer client)
        {
            IList<string> lst = new List<string>();

            foreach (KeyValuePair<string, ClientPer> item in dicID2Client)
            {
                if (item.Value == null)
                {
                    Debug.LogError("注意下这里");
                }

                if (item.Value != null && client == item.Value)
                {
                    lst.Add(item.Key);
                }
            }

            foreach (var id in lst)
            {
                dicID2User.Remove(id);
                dicID2Client.Remove(id);
            }
        }
    }

}
